﻿/********************************************************************
	created:	2011/05/12
	created:	12:5:2011   21:08
	filename: 	D:\My Documents\Visual Studio 2010\Projects\IntranetMessager\ServiceLibrary\OnlineUserRepository.cs
	file path:	D:\My Documents\Visual Studio 2010\Projects\IntranetMessager\ServiceLibrary
	file base:	OnlineUserRepository
	file ext:	cs
	author:		
	
	purpose:	在线用户信息管理
*********************************************************************/
using System;
using System.Collections.Generic;
using System.Threading;
using CommonLibrary;

namespace ServiceLibrary
{
    internal class OnlineUserRepository
    {
        private readonly Dictionary<int, IClientCallBack> _onlineUsersinfo =
            new Dictionary<int, IClientCallBack>();

        private readonly ReaderWriterLockSlim _rwLocker = new ReaderWriterLockSlim();

        public OnlineUserRepository()
        {
            ServiceRepository.Printf("Model - UserRepository - init OK");
        }

        internal bool IsUserSignIn(int uid)
        {
            _rwLocker.EnterReadLock();
            try
            {
                return _onlineUsersinfo.ContainsKey(uid);
            }
            finally
            {
                _rwLocker.ExitReadLock();
            }
        }

        internal bool AddNew(int uid, IClientCallBack channel)
        {
            _rwLocker.EnterWriteLock();
            try
            {
                if (_onlineUsersinfo.ContainsKey(uid))
                {
                    _onlineUsersinfo.Remove(uid);
                }
                _onlineUsersinfo.Add(uid, channel);
                return true;
            }
            catch (Exception ex)
            {
                ServiceRepository.Printf("onlineUsersinfo add error", ex);
                return false;
            }
            finally
            {
                _rwLocker.ExitWriteLock();
            }
        }

        internal void Remove(int uid)
        {
            _rwLocker.EnterWriteLock();
            try
            {
                _onlineUsersinfo.Remove(uid);
                return;
            }
            catch (Exception ex)
            {
                ServiceRepository.Printf("onlineUsersinfo remove error", ex);
                return;
            }
            finally
            {
                _rwLocker.ExitWriteLock();
            }
        }

        internal void GetChannel(int uid, out IClientCallBack channel)
        {
            _rwLocker.EnterReadLock();
            try
            {
                _onlineUsersinfo.TryGetValue(uid, out channel);
                return;
            }
            catch (Exception ex)
            {
                ServiceRepository.Printf("onlineUsersinfo get channel error", ex);
                channel = null;
                return;
            }
            finally
            {
                _rwLocker.ExitReadLock();
            }
        }

        internal UserInfoClass[] GetAll()
        {
            var userInfos = new UserInfoClass[_onlineUsersinfo.Count];
            var uids = new int[_onlineUsersinfo.Count];
            int i = 0;
            _rwLocker.EnterReadLock();
            try
            {
                _onlineUsersinfo.Keys.CopyTo(uids, 0);
                foreach (int uid in uids)
                {
                    userInfos[i] = new UserInfoClass {UserName = DataManagement.QueryUserName(uid), UserId = uid};
                    i++;
                }
                return userInfos;
            }
            finally
            {
                _rwLocker.ExitReadLock();
            }
        }
    }
}